/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_rticonnextdds/converter/SampleInfoConverter.hpp"
#include "dal_rticonnextdds/converter/InstanceHandleConverter.hpp"
#include "dal_rticonnextdds/converter/GuidConverter.hpp"

SampleInfoConverter* SampleInfoConverter::singleton_ = nullptr;

SampleInfoConverter::SampleInfoConverter() {}

SampleInfoConverter* SampleInfoConverter::GetInstance()
{
    if (singleton_ == nullptr) {
        singleton_ = new SampleInfoConverter();
    }
    return singleton_;
}

dal_ret_t SampleInfoConverter::ConvertSampleInfoFromRtiddsToDal(dal_sample_info_t* d_sampleinfo,
                                                                 DDS_SampleInfo* r_sampleinfo)
{
    switch (r_sampleinfo->sample_state) {
        case DDS_READ_SAMPLE_STATE:
            d_sampleinfo->sample_state = DAL_READ_SAMPLE_STATE;
            break;

        case DDS_NOT_READ_SAMPLE_STATE:
            d_sampleinfo->sample_state = DAL_NOT_READ_SAMPLE_STATE;
            break;
    }

    switch (r_sampleinfo->view_state) {
        case DDS_NEW_VIEW_STATE:
            d_sampleinfo->view_state = DAL_NEW_VIEW_STATE;
            break;

        case DDS_NOT_NEW_VIEW_STATE:
            d_sampleinfo->view_state = DAL_NOT_NEW_VIEW_STATE;
            break;
    }

    switch (r_sampleinfo->instance_state) {
        case DDS_ALIVE_INSTANCE_STATE:
            d_sampleinfo->instance_state = DAL_ALIVE_INSTANCE_STATE;
            break;

        case DDS_NOT_ALIVE_DISPOSED_INSTANCE_STATE:
            d_sampleinfo->instance_state = DAL_NOT_ALIVE_DISPOSED_INSTANCE_STATE;
            break;

        case DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE:
            d_sampleinfo->instance_state = DAL_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE;
            break;
    }

    d_sampleinfo->source_timestamp = {
        r_sampleinfo->source_timestamp.sec, 
        r_sampleinfo->source_timestamp.nanosec
    };

    InstanceHandleConverter::GetInstance()->ConvertInstanceHandleFromRtiddsToDal(
        &r_sampleinfo->instance_handle, &d_sampleinfo->instance_handle);

    InstanceHandleConverter::GetInstance()->ConvertInstanceHandleFromRtiddsToDal(
        &r_sampleinfo->publication_handle, &d_sampleinfo->publication_handle);

    d_sampleinfo->disposed_generation_count = (int32_t)r_sampleinfo->disposed_generation_count;
    d_sampleinfo->no_writers_generation_count = (int32_t)r_sampleinfo->no_writers_generation_count;
    d_sampleinfo->sample_rank = (int32_t)r_sampleinfo->sample_rank;
    d_sampleinfo->generation_rank = (int32_t)r_sampleinfo->generation_rank;
    d_sampleinfo->absolute_generation_rank = (int32_t)r_sampleinfo->absolute_generation_rank;
    if (r_sampleinfo->valid_data) {
        d_sampleinfo->valid_data = 1;
    }
    else {
        d_sampleinfo->valid_data = 0;
    }

    InstanceHandleConverter::GetInstance()->ConvertInstanceHandleFromRtiddsToDal(
        &r_sampleinfo->instance_handle, &d_sampleinfo->instance_handle);

    InstanceHandleConverter::GetInstance()->ConvertInstanceHandleFromRtiddsToDal(
        &r_sampleinfo->publication_handle, &d_sampleinfo->publication_handle);

    return DAL_RETCODE_OK;
}
